/**
 * @author cprobert
 */
var prefsUI = {
	soundFile: Object,
	init: function(){
		window.nativeWindow.visible = true;
		window.nativeWindow.activate(); 
		window.opener.sonicAlert.trace( "Loading preferences UI" );
		
		$('#monitorURL').val(window.opener.prefs.monitorURL);
		$('#title').val(window.opener.prefs.title);
		$('input#interval').val(window.opener.prefs.interval);
		$('input#soundFile').val(window.opener.prefs.soundFile);
		var enableSound = window.opener.prefs.enableSound;
		$('input#enableSound').attr( 'checked', enableSound ); //.checked = true;
		var enableToast = window.opener.prefs.enableToast;
		$('input#enableToast').attr('checked', enableToast);
		
		prefsUI.wireEvents();
	},
	wireEvents: function(){
		window.opener.sonicAlert.trace("wireing preference events...");
		prefsUI.soundFile = air.File.applicationDirectory;
		prefsUI.soundFile.addEventListener(air.Event.SELECT, prefsUI.doSoundSelect);
		
		$("input#btnPlay").click(function(){
			window.opener.sonicAlert.playSound(window.opener.prefs.soundFile);
		});
	
		$("input#browse").click(function(){
			window.opener.sonicAlert.trace('Browsing..');
			var filters = new Array();
			filters.push(new air.FileFilter("MP3 Files", "*.mp3"));
			prefsUI.soundFile.browseForOpen('Select an MP3 file', filters);
		});
		
		$("input#save").click(function(){
			prefsUI.save();
			window.close();
		});
		
		$("input#clear").click(function(){
			if(confirm("Are you sure you want to delete your preferences?")){
				air.EncryptedLocalStore.reset();
				window.opener.prefs.load();
				window.opener.sonicAlert.networkMonitor.available = false;
				window.opener.listenUp.stop();
				window.close(); //window.location.reload(true); //refresh page
			}
		});
	},
	doSoundSelect: function(e){
		window.opener.prefs.soundFile = prefsUI.soundFile.nativePath;
		$('input#soundFile').val(window.opener.prefs.soundFile);
		window.opener.sonicAlert.trace(window.opener.prefs.soundFile);
	},
	save: function(){
		var monitorURL = $('input#monitorURL').val();
		var interval = $('input#interval').val();
		var soundFile = $('input#soundFile').val();
		var title = $('#title').val();
		var enableSound = $('#enableSound').is(':checked');
		var enableToast = $('#enableToast').is(':checked');
		
		air.trace("enableSound: "+ enableSound);
		air.trace("enableToast: "+ enableToast);
		
		window.opener.prefs.save(monitorURL, interval, soundFile, title, enableSound, enableToast);
		window.opener.msg.toast("<h2>Preferences saved</h2>");
	}
};
